// pages/index/t_class/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    visible:false,
    className:'',
    cjvisible:false,
    dateVisible:false,
    dateText: '',
    dateValue: [],
    citys: [ ],
    studentId:'',
    subject:'',
    achievement:'',
    dateTime:'',
    mode: '',
    datetimeVisible: false,
    datetime: new Date('2023-01-01').getTime(), // 支持时间戳传入
    dateText: '',
    homeWorkId: '' ,
    // 指定选择区间起始值
    start: '2000-01-01 00:00:00',
    end: '2030-09-09 12:12:12',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
    

  },
  getList(){
    wx.request({
      url: 'http://localhost:9090/api/sysClass/getClassInfo',
      data:{
        userId:wx.getStorageSync('userInfo').user.id
      },
      method:'get',
      success:res=>{
        if(res.data.code == 200){
          this.setData({
            list:res.data.data
          })
        }
      }
    })
  },
  // 详情
  toDetail(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/index/t_class/stufent/index?student='+ encodeURIComponent(JSON.stringify(e.currentTarget.dataset.student)),
    })
  },
  // 创建班级
  addClass(){
    this.setData({
      visible:true
    })
  },
  blurInput(e){
    console.log(e)
    this.setData({
      className:e.detail.value
    })
  },
  // 取消
  cancelClass(){
    this.setData({
      visible:false
    })
  },
  // 确认创建
  confirmClass(){
    var code = '';
    for(var i=0;i<6;i++){
        code += parseInt(Math.random()*10);
    }
    console.log(code);
    if(this.data.className == ''){
      wx.showToast({
        title: '请填写班级名称！',
        icon:'none'
      })
      return
    }
    let list = this.data.list
    wx.request({
      url: 'http://localhost:9090/api/sysClass/createClass',
      data:{
        name:this.data.className,
        classKey: code,
        userId:wx.getStorageSync('userInfo').user.id
      },
      method:'post',
      success:res=>{
        if(res.data.code == 200){
          wx.setStorageSync('userInfo', res.data.data);
          wx.setStorageSync('userRank', res.data.data.user.identity);
          wx.showToast({
            title: '创建成功！',
            icon:'none'
          })
          this.getList()
          this.setData({
            visible:false,
            list: []
          })
        }
      }
    })
    
  },
  // 打卡
  toDaka(e){
    console.log(e)
    
    wx.showModal({
      title:'提示',
      content:'是否发布今日打卡任务？',
      success:res=>{
        if(res.confirm){
            wx.request({
              url: 'http://localhost:9090/api/sysClass/clockIn',
              data:{
                id: e.currentTarget.dataset.id
              },
              method:'POST',
              success:ress=>{
                if(ress.data.code == 200){
                  wx.showToast({
                    title: '发布成功！',
                    icon:'none'
                  })
                }else{
                  wx.showToast({
                    title: ress.data.msg,
                    icon:'none'
                  })
                }
              }
            })
        }
      }
    })
    // wx.navigateTo({
    //   url: '/pages/index/daka/index',
    // })
  },
  // 打开记录
  toDakaLog(e){
    wx.navigateTo({
      url: '/pages/index/t_class/logss/index?id='+e.currentTarget.dataset.id,
    })
  },
  // 成绩
  chengji(e){
    wx.request({
      url: 'http://localhost:9090/api/joinClass/selectStudent',
      method:'GET',
      data:{
        classId: e.currentTarget.dataset.id
      },
      success:res=>{
        res.data.data.forEach(item=>{
          item.label = item.name
          item.value = item.id
        })
        this.setData({
          citys: res.data.data,
          dateTime:'',
          subject:'',
          achievement:'',
          studentId:'',
          cjvisible:true
        })
      }
    })
   
  },
  blursubject(e){
    this.setData({
      subject:e.detail.value
    })
  },
  blurachievement(e){
    this.setData({
      achievement:e.detail.value
    })
  },
  onStudentPicker(){
    this.setData({
      dateVisible:true
    })
  },
  onColumnChange(e){
    console.log(e)
  },
  // 选择同学
  onconfimChange(e){
    console.log('1',e)
    this.setData({
      dateText:this.data.citys[e.detail.columns[0].index].label,
      studentId:this.data.citys[e.detail.columns[0].index].id
    })
  },
  onPickerCancel(e){
    this.setData({
      dateVisible:false
    })
  },
  showPicker(e) {
    console.log(e)
    this.setData({
      datetimeVisible: true,
    });
  },
  hidePicker() {
    this.setData({
      datetimeVisible: false,
    });
  },
  ontimeConfirm(e) {
    const { value } = e.detail;
    console.log('confim', value);
    this.setData({
      datetimeVisible:false,
      dateTime: value,
    });
    this.hidePicker();
  },
  // 创建成绩
  cancelCJ(){
    console.log('取消')
    this.setData({
      cjvisible:false
    })
  },
  confirmCJ(){
    if(this.data.dateText ==''){
      wx.showToast({
        title: '请选择同学',
        icon:'none'
      })
      return
    }else if(this.data.dateTime == ''){
      wx.showToast({
        title: '请选择考试时间',
        icon:'none'
      })
      return
    }else if(this.data.subject == ''){
      wx.showToast({
        title: '请输入科目',
        icon:'none'
      })
      return
    }else if(this.data.achievement == ''){
      wx.showToast({
        title: '请输入成绩',
        icon:'none'
      })
      return
    }
    let parma = {
      studentId:this.data.studentId,
      subjet:this.data.subject,
      achievement:this.data.achievement,
      time:this.data.dateTime
    }
    console.log('参数',parma)
    wx.request({
      url: 'http://localhost:9090/api/studentResult/resultInsert',
      data:{
        userId:this.data.studentId,
        fraction:this.data.achievement,
        subject:this.data.subject,
        examTime:this.data.dateTime+' 08:00:00'
      },
      method:'post',
      success:res=>{
        if(res.data.code == 200){
          wx.showToast({
            title: '发布成功！',
            icon:'none'
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })
    this.setData({
      cjvisible:false
    })
  },
  // 发布公告
  gonggao(e){
    wx.navigateTo({
      url: '/pages/index/t_class/gonggao/index?id='+e.currentTarget.dataset.id,
    })
  },
  // 发布作业  
  addHomeWork(e) { 
    console.log(1);
    
    wx.navigateTo({
      url: './addHomeWork/addHomeWork?id='+e.currentTarget.dataset.id ,
    })
  },
  getHomeWorkList(e){
    console.log(1);
    // 只查当前班级的作业
    wx.navigateTo({
      url: './HomeWorkList/HomeWorkList?classId=' + e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})